using System.Drawing;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// This is a control for editing a unique constraint details and its property details.
    /// </summary>
    public class DMUniqueConstraintEditorControl : UserControlWin, IBOEditorControl
    {
        private IPanelInfo _detailPanelInfo;
        private readonly IControlFactory _controlFactory;
        private DMUniqueConstraintPropertiesControl _uniqueConstraintPropertiesControl;

        public DMUniqueConstraintEditorControl(IControlFactory controlFactory)
        {
            _controlFactory = controlFactory;
            InitialiseControl();
        }

        private void InitialiseControl()
        {
            PanelBuilder panelBuilder = new PanelBuilder(_controlFactory);

            _detailPanelInfo = panelBuilder.BuildPanelForForm
                (ClassDef.Get<DMUniqueConstraint>().UIDefCol["default"].UIForm);

            IGroupBox groupBox = _controlFactory.CreateGroupBox("");
            groupBox.Controls.Add(_detailPanelInfo.Panel);
            int height = _detailPanelInfo.MinimumPanelHeight + 20;
            groupBox.Height = height;
            int width = _detailPanelInfo.Panel.MinimumSize.Width + 10;
            groupBox.Width = width;

            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(groupBox, BorderLayoutManager.Position.North);

            _uniqueConstraintPropertiesControl = new DMUniqueConstraintPropertiesControl(_controlFactory);
            layoutManager.AddControl(_uniqueConstraintPropertiesControl, BorderLayoutManager.Position.Centre);
            this.MinimumSize = new Size(width, height + _uniqueConstraintPropertiesControl.MinimumSize.Height);
        }

        public IPanelInfo DetailPanelInfo
        {
            get { return _detailPanelInfo; }
        }

        public IBusinessObject BusinessObject
        {
            get { return _detailPanelInfo.BusinessObject; }
            set
            {
                _detailPanelInfo.BusinessObject = value;
                this.PropertiesControl.BusinessObject = value;
            }
        }

        public IBusinessObjectControl PropertiesControl
        {
            get { return _uniqueConstraintPropertiesControl; }
        }

        public void DisplayErrors()
        {
            DetailPanelInfo.ApplyChangesToBusinessObject();
        }

        public void ClearErrors()
        {
            DetailPanelInfo.ClearErrorProviders();
        }

        #region Implementation of IBOEditorControl

        /// <summary>
        ///             Applies any changes that have occured in any of the Controls on this control's to their related
        ///             Properties on the Business Object.
        /// </summary>
        public void ApplyChangesToBusinessObject()
        {
            this._detailPanelInfo.ApplyChangesToBusinessObject();
        }

        /// <summary>
        ///             Does the business object controlled by this control or any of its Aggregate or Composite children have and Errors.
        /// </summary>
        public bool HasErrors
        {
            get { throw new System.NotImplementedException(); }
        }

        /// <summary>
        ///             Does the Business Object controlled by this control or any of its Aggregate or Composite children have and warnings.
        /// </summary>
        public bool HasWarning
        {
            get { throw new System.NotImplementedException(); }
        }

        /// <summary>
        ///              Returns a list of all warnings for the business object controlled by this control or any of its children.
        /// </summary>
        public ErrorList Errors
        {
            get { throw new System.NotImplementedException(); }
        }

        /// <summary>
        ///             Does the business object being managed by this control have any edits that have not been persisted.
        /// </summary>
        /// <returns>
        /// </returns>
        public bool IsDirty
        {
            get { throw new System.NotImplementedException(); }
        }

        /// <summary>
        ///             Returns a list of all warnings for the business object controlled by this control or any of its children.
        /// </summary>
        /// <returns>
        /// </returns>
        public ErrorList Warnings
        {
            get { throw new System.NotImplementedException(); }
        }

        #endregion
    }
}